package bb.lanxing.lib.devices.ble.ble.base;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
//import com.alipay.sdk.packet.d;
import bb.lanxing.lib.devices.ble.utils.UUIDExtKt;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import kotlin.Metadata;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.jvm.internal.Intrinsics;
import kotlinx.coroutines.DelayKt;
/* compiled from: BaseBleService.kt */

public abstract class BaseBleService {
    private List<BaseBleCharacteristic> bleCharacteristics;
    private final BaseBleSensorDevice device;
    private boolean isCaptured;
    private BluetoothGatt rawGatt;
    private BluetoothGattService rawService;

    public abstract Object initialization(Continuation<? super Unit> continuation);

    public abstract UUID targetUUID();

    public BaseBleService(BaseBleSensorDevice device) {
//        Intrinsics.checkNotNullParameter(device, "device");
        this.device = device;
        device.setUpServices(this);
        this.bleCharacteristics = new ArrayList();
    }

    public final BaseBleSensorDevice getDevice() {
        return this.device;
    }

    public final boolean isCaptured() {
        return this.isCaptured;
    }

    public final void setCaptured(boolean z) {
        this.isCaptured = z;
    }

    public final BluetoothGatt getRawGatt() {
        return this.rawGatt;
    }

    public final void setRawGatt(BluetoothGatt bluetoothGatt) {
        this.rawGatt = bluetoothGatt;
    }

    public final BluetoothGattService getRawService() {
        return this.rawService;
    }

    public final void setRawService(BluetoothGattService bluetoothGattService) {
        this.rawService = bluetoothGattService;
    }

    public final List<BaseBleCharacteristic> getBleCharacteristics() {
        return this.bleCharacteristics;
    }

    public final void setBleCharacteristics(List<BaseBleCharacteristic> list) {
        Intrinsics.checkNotNullParameter(list, "<set-?>");
        this.bleCharacteristics = list;
    }

    public final boolean captureServices(BluetoothGatt gatt) {
        Unit unit;
        BluetoothGattService obj;
        boolean z;
//        Intrinsics.checkNotNullParameter(gatt, "gatt");
        List<BluetoothGattService> services = gatt.getServices();
//        Intrinsics.checkNotNullExpressionValue(services, "gatt.services");
        Iterator<BluetoothGattService> it2 = services.iterator();
        while (true) {
            unit = null;
            if (!it2.hasNext()) {
                obj = null;
                break;
            }
            obj = it2.next();
            BluetoothGattService bluetoothGattService = obj;
            if (!bluetoothGattService.getUuid().equals(targetUUID())) {
                UUID uuid = bluetoothGattService.getUuid();
//                Intrinsics.checkNotNullExpressionValue(uuid, "it.uuid");
                if (!Intrinsics.areEqual(UUIDExtKt.keyWord(uuid), UUIDExtKt.keyWord(targetUUID()))) {
                    z = false;
                    continue;
//                    if (z) {
//                        break;
//                    }
                }
            }
            z = true;
            continue;
//            if (z) {
//            }
        }
        BluetoothGattService bluetoothGattService2 = obj;
        if (bluetoothGattService2 != null) {
            this.rawGatt = gatt;
            this.rawService = bluetoothGattService2;
            this.isCaptured = true;
            unit = Unit.INSTANCE;
        }
        if (unit == null) {
            this.isCaptured = false;
        }
        return this.isCaptured;
    }

    public final void onCharacteristicValueRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int i) {
//        Intrinsics.checkNotNullParameter(gatt, "gatt");
//        Intrinsics.checkNotNullParameter(characteristic, "characteristic");
        for (BaseBleCharacteristic baseBleCharacteristic : this.bleCharacteristics) {
            if (!characteristic.getUuid().equals(baseBleCharacteristic.targetUUID())) {
                UUID uuid = characteristic.getUuid();
//                Intrinsics.checkNotNullExpressionValue(uuid, "characteristic.uuid");
                if (Intrinsics.areEqual(UUIDExtKt.keyWord(uuid), UUIDExtKt.keyWord(baseBleCharacteristic.targetUUID()))) {
                }
            }
            baseBleCharacteristic.onCharacteristicValueRead(gatt, characteristic, i);
        }
    }

    public final void onCharacteristicValueChange(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
//        Intrinsics.checkNotNullParameter(gatt, "gatt");
//        Intrinsics.checkNotNullParameter(characteristic, "characteristic");
        for (BaseBleCharacteristic baseBleCharacteristic : this.bleCharacteristics) {
            if (!characteristic.getUuid().equals(baseBleCharacteristic.targetUUID())) {
                UUID uuid = characteristic.getUuid();
//                Intrinsics.checkNotNullExpressionValue(uuid, "characteristic.uuid");
                if (Intrinsics.areEqual(UUIDExtKt.keyWord(uuid), UUIDExtKt.keyWord(baseBleCharacteristic.targetUUID()))) {
                }
            }
            baseBleCharacteristic.onCharacteristicValueChange(gatt, characteristic);
        }
    }

    public final Object delayInPeer(Continuation<? super Unit> continuation) {
        Object delay = DelayKt.delay(180L, continuation);
        return delay == IntrinsicsKt.getCOROUTINE_SUSPENDED() ? delay : Unit.INSTANCE;
    }

    public final void log(String content) {
//        Intrinsics.checkNotNullParameter(content, "content");
        this.device.d(content);
    }
}
